<template>
  <el-dialog :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item prop="type" :label="$t('MessageTemplate.type')">
            <el-select v-model="form.type" clearable>
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_message_template_type"
                         :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item :label="$t('MessageTemplate.state')" prop="state">
            <el-radio-group v-model="form.state">
              <el-radio :key="index" :label="item.value" border v-for="(item, index) in common_state_enable">
                {{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20 box">
          <div class="title">{{ $t('MessageTemplate.title') }}</div>
          <div v-for="(item,index) in $languages" :key="index" class="box-content">
            <el-form-item :prop="`title.${item.value}`">
            <div>{{ item.label }}</div>
            <el-input type="textarea"
                      maxlength="255" :rows="5"
                      clearable show-word-limit
                      v-model="form.title[item.value]"
                      :placeholder="$t('MessageTemplate.inputTitleTip')"
            />
            </el-form-item>
          </div>
        </el-col>

        <el-col :span="12" class="mb20 box">
          <div class="title">{{ $t('MessageTemplate.content') }}</div>
          <div v-for="(item,index) in $languages" :key="index" class="box-content">
            <el-form-item :prop="`content.${item.value}`">
            <div>{{ item.label }}</div>
            <el-input type="textarea"
                      maxlength="255" :rows="5"
                      clearable show-word-limit
                      v-model="form.content[item.value]"
                      :placeholder="$t('MessageTemplate.inputContentTip')"
            />
            </el-form-item>
          </div>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{
              $t('common.confirmButtonText')
            }}</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="MessageTemplateDialog">
import {useMessage} from "/@/hooks/message";
import {getObj, addObj, putObj} from '/@/api/bizs/MessageTemplate'

const emit = defineEmits(['refresh']);

// 定义i18n
import {useI18n} from 'vue-i18n';

const {t} = useI18n();

// 定义字典
import {useDict} from '/@/hooks/dict';

const {biz_message_template_type} = useDict('biz_message_template_type')
const {common_state_enable} = useDict('common_state_enable')

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const dataFormRef = ref();

// 提交表单数据
const form = reactive({
  id: '',
  type: '',
  state: '',
  title: {
    'zh-cn': '',
    'en': '',
  },
  content: {
    'zh-cn': '',
    'en': '',
  },
});

// 定义校验规则
import {rule} from '/@/utils/validate';

const dataRules = ref({
  type: [{required: true, message: t('MessageTemplate.inputTypeTip'), trigger: 'blur'}],
  state: [{required: true, message: t('MessageTemplate.inputStateTip'), trigger: 'blur'}],
  'title.zh-cn': [{required: true, message: t('MessageTemplate.inputTitleTip'), trigger: 'blur'}],
  'title.en': [{required: true, message: t('MessageTemplate.inputTitleTip'), trigger: 'blur'}],

  'content.zh-cn': [{required: true, message: t('MessageTemplate.inputContentTip'), trigger: 'blur'}],
  'content.en': [{required: true, message: t('MessageTemplate.inputContentTip'), trigger: 'blur'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 初始化表单
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 提交表单
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 暴露变量
defineExpose({
  openDialog
});
</script>

<style scoped lang="scss">

.title {
  height: 32px;
  line-height: 32px;

  &::before {
    content: '*';
    color: var(--el-color-danger);
    margin-right: 4px;
  }

}

.box {
  padding-left: 54px !important;

  .box-content {
    width: 100%;
    margin-bottom: 20px;
  }

}

</style>
